package com.mdd.maduoduo.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.mdd.maduoduo.R;
import com.mdd.maduoduo.entity.CheckVersionEntity;

public class AlertUtils {

    private static AlertDialog alertDialog;

    public static AlertDialog normalAlert(final Activity context, String title, String content,View.OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_normal, null);
        TextView textView = view.findViewById(R.id.title);
        TextView tvContent = view.findViewById(R.id.content);
        textView.setText(title);

        tvContent.setText(content);
        builder.setView(view);
        alertDialog = builder.create();
        Window window = alertDialog.getWindow();
        view.findViewById(R.id.cancel).setOnClickListener(listener);
        view.findViewById(R.id.sure).setOnClickListener(v -> alertDialog.dismiss());
        alertDialog.show();

        WindowManager m = context.getWindowManager();
        //为获取屏幕宽、高
        Display d = m.getDefaultDisplay();
        //获取对话框当前的参数值
        WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();
        p.width = (int) (d.getWidth() * 0.8);
        alertDialog.getWindow().setAttributes(p);
        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return alertDialog;
    }

    public static AlertDialog normalBtnAlert(final Activity context, String title, String content,String left,String right,View.OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_normal, null);
        TextView textView = view.findViewById(R.id.title);
        TextView tvContent = view.findViewById(R.id.content);
        TextView cancel = view.findViewById(R.id.cancel);
        TextView sure  = view.findViewById(R.id.sure);
        textView.setText(title);

        tvContent.setText(content);
        builder.setView(view);
        alertDialog = builder.create();
        Window window = alertDialog.getWindow();
        cancel.setText(left);
        sure.setText(right);
        sure.setOnClickListener(listener);
        cancel.setOnClickListener(v -> alertDialog.dismiss());
        alertDialog.show();

        WindowManager m = context.getWindowManager();
        //为获取屏幕宽、高
        Display d = m.getDefaultDisplay();
        //获取对话框当前的参数值
        WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();
        p.width = (int) (d.getWidth() * 0.8);
        alertDialog.getWindow().setAttributes(p);
        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return alertDialog;
    }

    public static AlertDialog tipsAlert(final Activity context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_tips, null);

        TextView sure  = view.findViewById(R.id.sure);

        builder.setView(view);
        alertDialog = builder.create();
        Window window = alertDialog.getWindow();
        sure.setOnClickListener(view1 -> alertDialog.dismiss());
        alertDialog.show();
        WindowManager m = context.getWindowManager();
        //为获取屏幕宽、高
        Display d = m.getDefaultDisplay();
        //获取对话框当前的参数值
        WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();
        p.width = (int) (d.getWidth() * 0.8);
        alertDialog.getWindow().setAttributes(p);
        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return alertDialog;
    }

    public static AlertDialog normaEtlAlert(final Activity context, String title,SelectRoleListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_normal_et, null);
        TextView textView = view.findViewById(R.id.title);
        EditText etRole = view.findViewById(R.id.etRole);
        textView.setText(title);

        builder.setView(view);
        alertDialog = builder.create();
        Window window = alertDialog.getWindow();
        view.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String role = etRole.getText().toString();
                if (!role.isEmpty()){
                    listener.sureSelect(role);
                }else {
                    ToastUtils.showLongToast(context,"请输入身份");
                }

            }
        });
        view.findViewById(R.id.cancel).setOnClickListener(v -> alertDialog.dismiss());
        alertDialog.show();

        WindowManager m = context.getWindowManager();
        //为获取屏幕宽、高
        Display d = m.getDefaultDisplay();
        //获取对话框当前的参数值
        WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();
        p.width = (int) (d.getWidth() * 0.8);
        alertDialog.getWindow().setAttributes(p);
        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return alertDialog;
    }

    public static AlertDialog showUpdateAlert(Activity context, CheckVersionEntity entity, DownloadListener listener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_update, null);
        TextView tvAskMeLater = view.findViewById(R.id.tvAskMeLater);
        TextView tvUpgradeNow = view.findViewById(R.id.tvUpgradeNow);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) TextView tvRemark = view.findViewById(R.id.tvRemark);
        tvRemark.setText(entity.data.remark);
        builder.setView(view);
        alertDialog = builder.create();
        if (entity.data.checkResult==2){
            alertDialog.setCanceledOnTouchOutside(false);
            //强制更新
            tvAskMeLater.setVisibility(View.GONE);
            alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    AppManager.getAppManager().finishAllActivity();
                }
            });

        }else {
            alertDialog.setCanceledOnTouchOutside(true);
            tvAskMeLater.setVisibility(View.VISIBLE);
            tvAskMeLater.setOnClickListener(view1 -> alertDialog.dismiss());
        }

        tvUpgradeNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.sureSelect(entity.data.downloadUrl);
            }
        });
        alertDialog.show();

//        // 设置窗口参数
//        WindowManager m = context.getWindowManager();
//        //为获取屏幕宽、高
//        Display d = m.getDefaultDisplay();
//        //获取对话框当前的参数值
//        WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();
//        p.width = d.getWidth();
//        p.height = d.getWidth();
//        alertDialog.getWindow().setAttributes(p);
        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return alertDialog;
    }

    public interface DownloadListener {

        void sureSelect(String url);
    }

    public interface SelectRoleListener {
        void sureSelect(String role);
    }


    public static void dismiss() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
            alertDialog = null;
        }
    }
}
